<?php
/**
 * Read in all files that corresponds to the googleId and encodes it to an Json
 * 
 * @author Sean
 * @return a json with all names of the files
 */
 
include ("connect.php");
include ("config.php");

$GoogleID = $_GET['googleId'];
$GoogleID = mysql_real_escape_string($GoogleID);

$SessionID = $_GET['sessionId'];
$SessionID = mysql_real_escape_string($SessionID);

// Checks if the sessionid is valid
$result = mysql_query("SELECT sessionID FROM users WHERE GoogleID = '" . $GoogleID . "'");
while ($row = mysql_fetch_array($result)) {
   $DB_SessionID = $row["sessionID"];
}

if($SessionID == $DB_SessionID){
	
	$pathToFiles = $pathToUsersFiles.$GoogleID."/";
	
	// open the directory
	$dir = opendir( $pathToFiles );
	$array = array();
	$totFileSize = 0;
	// loop through it, looking for all files:
	while (false !== ($fname = readdir( $dir ))) {
	
		if ( $fname != "." && $fname != "thumbs" && $fname != ".." && $fname != "gallery" )
		{
		$fileSize = 0;
		$fileSize = intval(filesize($pathToFiles.$fname)/1024);
		$totFileSize = $totFileSize + $fileSize;
			// creates the array with the filenames
			$array[] = array("fileName"=>$GoogleID."/thumbs/".$fname, 
								"fileType"=>end(explode('.', $fname)), 
								"fileSize"=>$fileSize,
								"fileModified"=>date("Y-m-d H:i:s", filemtime( $pathToFiles.$fname) ) );
		}
	}	
	$array[] = array("totFileSize"=>$totFileSize);
	
	$json = str_replace('\/', '/', json_encode($array));

	header('Access-Control-Allow-Origin: *');
	
	// the response to the client
	echo $json; 
	
	closedir($dir);
}
else{
	header('Location: http://sean.servern.org/webdrop/Webdrop.html?#page=Login');
}

?>